package cn.faifai.desktop.api;

import cn.faifai.common.component.R;
import cn.faifai.common.constant.Constants;
import cn.faifai.common.page.TableDataInfo;
import cn.faifai.framework.utils.fetcher.fileIdFetcher.FileIdFetcherFactory;
import cn.faifai.tagapi.dto.*;
import cn.faifai.tagapi.entity.IFile;
import cn.faifai.tagapi.entity.Tag;
import cn.faifai.tagapi.enums.fileSaveCode;
import cn.faifai.tagapi.service.FileUrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class FileUrlController {

    @Autowired
    private FileUrlService fileUrlService;

    /**
     * 获取文件元信息
     */
    public R getFileUrlInfo(@PathVariable Long id){
        return R.ok(fileUrlService.getFileUrlInfo(id));
    }

    public R getSubPathm(@RequestBody FileUrlDto fileUrlDto){
        return R.ok(fileUrlService.getSubPath(fileUrlDto.getFileUrl()));
    }

    public List<String> getSubPath(String path){
        if (path.equals(Constants.MY_COMPUTER)) return Arrays.stream(File.listRoots()).map(File::getAbsolutePath).collect(Collectors.toList());
        List<String> subPath = fileUrlService.getSubPath(/*path.replaceFirst("/", "")*/path);
//        subPath.forEach(item->item.replace("\\","/"));
//        subPath.forEach(System.out::println);
        return subPath;
    }

    public SubDirectoryDto getSubPath2(String path){
        if (path.equals(Constants.MY_COMPUTER)) return new SubDirectoryDto(null, Arrays.stream(File.listRoots()).map(File::getAbsolutePath).collect(Collectors.toList()));
        SubDirectoryDto subPath = fileUrlService.scanDirectory(path);
        return subPath;
    }

    public R registerFile(String path) throws IOException, NoSuchAlgorithmException {
        fileUrlService.registerFile(path);
        return R.ok();
    }

    public R openFile(String path) {
        fileUrlService.openFileDesktop(path);
        return R.ok();
    }

    /**
     * 标记单个文件
     */
    public R tagFile(@RequestBody FileUrlDto fileUrlDto) throws IOException, NoSuchAlgorithmException {
        Response checkResponse = fileUrlService.checkFileRegister(fileUrlDto.getFileUrl());
        Integer flag = checkResponse.getCode();
        IFile iFile = (IFile) checkResponse.getData();
        if (flag == fileSaveCode.FILE_NOT_EXIST.getCode()){
            Response registResponse = fileUrlService.registerFile(fileUrlDto.getFileUrl());
            flag = registResponse.getCode();
            iFile = (IFile) registResponse.getData();
            fileUrlDto.setId(((IFile)registResponse.getData()).getId());
        }
        if (flag == fileSaveCode.FILE_REGISTERED.getCode()){
            fileUrlDto.setId(iFile.getId());
            flag = fileUrlService.tagFile(fileUrlDto);
        }
        return flag == fileSaveCode.SUCCESS.getCode() ? R.ok() : R.fail(fileSaveCode.getMessage(flag));
    }

    // 置换文件标签
    public Boolean replaceTag(FileUrlDto fileUrlDto) throws IOException, NoSuchAlgorithmException {
        Response checkResponse = fileUrlService.checkFileRegister(fileUrlDto.getFileUrl());
        Integer flag = checkResponse.getCode();
        IFile iFile = (IFile) checkResponse.getData();
        if (flag == fileSaveCode.FILE_NOT_EXIST.getCode()){
            Response registResponse = fileUrlService.registerFile(fileUrlDto.getFileUrl());
            flag = registResponse.getCode();
            iFile = (IFile) registResponse.getData();
            fileUrlDto.setId(((IFile)registResponse.getData()).getId());
        }
        if (flag == fileSaveCode.FILE_REGISTERED.getCode()){
            fileUrlDto.setId(iFile.getId());
            flag = fileUrlService.replaceTag(fileUrlDto);
        }
        return flag == fileSaveCode.SUCCESS.getCode() ? true : false;
    }

    /**
     * 获取注册文件列表
     */
    public TableDataInfo getFileList(FileUrlQuery<IFile> query, PageQuery pageQuery){
        return fileUrlService.getFileList(query, pageQuery);
    }

    /**
     * 根据tagid查询分页查询文件
     */
    public TableDataInfo getFileListByTagId(@PathVariable Long tagId,@ModelAttribute PageQuery pageQuery){
        return fileUrlService.getFileListByTagId(tagId, pageQuery);
    }

    /**
     * 删除文件
     */
    public R deleteFile(@PathVariable List<Long> ids){
        fileUrlService.deleteRegisterFile(ids);
        return R.ok();
    }

    public Boolean hasSubPath(String path){
        return fileUrlService.hasSubPath(path);
    }

    /**
     * 获取文件标签信息
     */
    public List<TagDTO> getFileTags(String systemPath) {
        return fileUrlService.getFileTags(systemPath);
    }
}